package cn.waka.sys.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Node;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.object.Role;
import cn.waka.sys.admin.service.NodeService;
import cn.waka.sys.admin.service.RoleService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private NodeService nodeService;

    @RequestMapping(path = "/edit", name = "编辑角色")
    public ModelAndView edit(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Role role = roleService.findById(id);
        if (role == null) {
            role = new Role();
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", role);
        return mv;
    }

    @RequestMapping(path = "/save", name = "保存角色")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String title = req.getParameter("title");
        String desc = req.getParameter("desc");

        Role role = new Role();
        role.setId(id);
        role.setTitle(title);
        role.setDesc(desc);
        int rs = roleService.saveRole(role);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }


    @RequestMapping(path = "/index", name = "角色分页列表")
    public ModelAndView pageList(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        String login = req.getParameter("login");
        String uname = req.getParameter("uname");
        Map<String, Object> param = new HashMap<String, Object>();
        if (login != null && login.length() > 0) {
            param.put("login", "%" + login + "%");
        }
        if (uname != null && uname.length() > 0) {
            param.put("uname", "%" + uname + "%");
        }

        roleService.findPage(p, param);
        ModelAndView mv = new ModelAndView();
        mv.addObject("page", p);
        return mv;

    }

    @RequestMapping(path = "/updateStatus", name = "启用停用角色")
    public Result updateStatus(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String status = req.getParameter("status");
        int rs = roleService.updateStatus(id, status);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/del", name = "删除角色")
    public Result del(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = roleService.del(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/apply", name = "角色权限")
    public ModelAndView apply(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }

        Role role = roleService.findById(id);
        if (role == null) {
            role = new Role();
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", role);
        return mv;
    }

    @RequestMapping(path = "/getNode", produces = "application/json; charset=UTF-8")
    public Result getNode(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        try {
            List<Node> nodeTree = nodeService.getNodeTree(id);
            return ok(nodeTree);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-500, e.getMessage());
        }


    }

    @RequestMapping(path = "/saveNode", produces = "application/json; charset=UTF-8")
    public Result saveNode(HttpServletRequest req, HttpServletResponse res) {
        String[] nodes = req.getParameterValues("nodes[]");
        String role_id = req.getParameter("role_id");

        nodeService.saveNodes(role_id, nodes);
        roleService.removeCacheByKey(role_id);
        return error(1, "操作成功！");

    }

    @RequestMapping(path = "/cache", name = "刷新角色缓存")
    public Result refreshCache(HttpServletRequest req, HttpServletResponse res) {
        roleService.refreshCache();
        return error(1, "操作成功！");

    }
}
